<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('nomenclature_form.class.php');
require_once('nomenclature_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');


class Nomenclature_Form_Ctrl extends GForm_Ctrl_With_Object {

	protected function create_form() {
		$form = new nomenclature_Form();

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new Nomenclature_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		if (!ereg("^[a-z]{1}[a-z0-9_]{0,254}$", $this->form->get_field_value('hid'))) {
			$this->form->set_field_error('hid', 'invalid_hid');
			$ret = false;
		} else {
			$dbc = $this->get_dbc();
			$object = $this->get_object();
			if (!TCMS_Nomenclature_DBC::is_unique_hid($dbc, $object->get_id(), $this->form->get_field_value('hid'))) {
				$this->form->set_field_error('hid', 'duplicate_hid');
				$ret = false;
			}
		}

		return $ret;
	}


	protected function on_bad_submit() {

	}


	protected function create_object() {
		$object = new TCMS_nomenclature_DBC();

		return $object;
	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$form->set_field_value('name', $object->get_name());
		$form->set_field_value('hid', $object->get_hid());
	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$object->set_name(trim($form->get_field_value('name')));
		$object->set_hid($form->get_field_value('hid'));
	}
}